//
//  TopicViewController.m
//  TextApplication
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "TopicViewController.h"
#import "TopicTableViewCell.h"
#import "TAMacro.h"
#import "ZWYNetTool.h"
#import "ClassFirst.h" // model重用
#import "ClassifyData.h"
#import "ClassItems.h"

#import "CycleDetailViewController.h" // 重用wedView

#import "MBProgressHUD.h"
#import "MJRefresh.h"

@interface TopicViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ClassFirst *fist;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSMutableArray <ClassItems *>*dataArray;

@end

@implementation TopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [NSMutableArray array];
    self.page = 1;
    [self createTableView];
    [self createData];
}
// 上拉加载
- (void)upLoad {
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(createData)];
}

// 下拉刷新
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
           if (self.tableView.contentOffset.y < 0) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
            [hud hideAnimated:YES afterDelay:1];
            self.page = 1;
            NSLog(@"下拉刷新");
            [self createData];
        }
    }



- (void)createData {
   
    NSString *str1 = @"http://mobile.iliangcang.com/goods/shopSpecial/?page=";
    NSString *str2 = @"&count=5&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSString *str = [[str1 stringByAppendingString:[NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString:str2];
    NSLog(@"%ld", self.page);
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        self.fist = [ClassFirst modelWithDic:result];
        for (ClassItems *item in self.fist.data.items) {
            [self.dataArray addObject:item];
        }
        self.page = self.page + 1;

        NSLog(@"%ld", self.dataArray.count);
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 99, WIDTH, HEIGHT - 99 - 44) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 170;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self upLoad];
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.data = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CycleDetailViewController *detailVC = [[CycleDetailViewController alloc] init];
    detailVC.urlStr = self.dataArray[indexPath.row].topic_url;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
